<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>



  <script>
    var str = '';
    var level = prompt('请设置金字塔的层数');
      //  level = parseInt(level) && Number(level);
        if (isNaN(level)) {
      alert('金字塔的层数必须是纯数字');
    }
       for (var i = 1; i <= level; i++) {
      
      var blank = level - i;
      for (var k = 0; k < blank; ++k) {
        str += ' ';
      }
       var star = i * 2 - 1;
      for (var j = 0; j < star; j++) {
        str += '*';
      }
       str += '\n';
    }
  console.log(str);
  </script>  
</body>
</html>